Struct
Result<T, E>
union struct Result<T, E>
Namespace: System
Assembly: Raven.Core.dll
Source file: Result.rvn
Inheritance: object → ValueType → Result<T, E>
Implements: IPropagatable<Result<T, E>, T, E>
Represents either a result with a value, or an error. See also: Result<T, E>.
Usage
Use Result like so:
func Test(v: int) -> Result<int, string> {
if v > 3 {
return Ok(42)
}
Error("Wrong number")
}
Matching result in match expression:
let result = Test(2)
let r = result match {
Ok(let value) => "Ok: $value"
Error(let message) => "Error: '$message'"
}
Simple pattern:
if result is Ok(let value) {
// Omitted
} else if result is Error(let error) {
// Omitted
}
Type parameters
| Name | Description |
|---|---|
T |
The successful value type. |
E |
The error value type. |
Cases
2 cases
case Ok(value: T)
Represents a successful result value.
case Error(value: E)
Represents an error result.
Properties
6 items
val HasError: bool
Gets a value indicating whether this result is `Error`.
val HasOk: bool
Gets a value indicating whether this result is `Ok`.
val HasValue: bool
Represents either a result with a value, or an error. See also: [Result<T, E>](xref:T:System.Result`2).
val IsError: Option<E>
Gets the error value as an option.
val IsOk: Option<T>
Gets the successful value as an option.
val Value: object?
Represents either a result with a value, or an error. See also: [Result<T, E>](xref:T:System.Result`2).
Methods
19 items
static func FromResidual(residual: E) -> Result<T, E>
Constructs an error result from a propagated residual.
func GetEnumerator() -> IEnumerator<T>
Returns an enumerator over the zero-or-one successful values.
func Map<TResult>(mapper: T -> TResult) -> Result<TResult, E>
Projects the value inside `Ok` using `mapper`. `Error` stays `Error`.
func MapError<E2>(mapper: E -> E2) -> Result<T, E2>
Maps the error channel.
func Match<TResult>(ok: T -> TResult, error: E -> TResult) -> TResult
Matches the result and produces a value.
func OrElse(recover: E -> Result<T, E>) -> Result<T, E>
Recovers from an error by mapping it to a new result.
func Tap(action: T -> ()) -> Result<T, E>
Runs `action` if this is `Ok`, returning the original result.
func TapError(action: E -> ()) -> Result<T, E>
Runs `action` if this is `Error`, returning the original result.
func Then<TResult>(binder: T -> Result<TResult, E>) -> Result<TResult, E>
Monadic bind / flatMap: chains a `Result`-producing function.
func ToEnumerable() -> IEnumerable<T>
Converts `Ok(x)` to a single-item sequence and `Error` to an empty sequence.
virtual override func ToString() -> string
No summary available.
virtual override func ToString() -> string
Represents either a result with a value, or an error. See also: [Result<T, E>](xref:T:System.Result`2).
func TryGetOutput(out output: T) -> bool
Attempts to extract the successful output used by the propagation protocol.
func TryGetResidual(out residual: E) -> bool
Attempts to extract the residual used by the propagation protocol.
func UnwrapError() -> E
Returns the error value, or throws when this result is `Ok`.
func UnwrapOr(defaultValue: T) -> T
Returns the successful value, or `defaultValue` for an error.
func UnwrapOrDefault() -> T
Returns the successful value, or the default value for an error.
func UnwrapOrElse(factory: () -> T) -> T
Unwrap the value if Ok, otherwise return the value from `factory()`.
func UnwrapOrThrow() -> T
Returns the successful value, or throws for an error.